﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Data.Linq;
using System.Collections.Generic;

namespace Hisun.View.ReportQuery.BaseReport
{
    public partial class Report_CPZYZXCBHZB_Base : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        string CenterIdArray = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist(DropDownList_Year, DropDownList_Month, Application["accountPeriod"].ToString());


                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();

                CheckBox_WithInventary.Checked = true;

                DropDownList_Product.Items.Clear();
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

                DropDownList_Product.Items.Clear();
                if (CenterIdArray == "")
                {

                }
                else
                {
                    string[] CenterIDs = CenterIdArray.Split(',');


                    var myProductList = (from a in db.TABC_JS_ProductInProcess
                                         join b in db.TABC_JS_Product on a.产品ID equals b.产品账户ID
                                         where CenterIDs.Contains(a.作业中心ID.ToString())
                                         && a.可用 == true
                                         orderby a.产品ID
                                         select new
                                         {
                                             a.产品ID,
                                             b.产品账户名称
                                         }).Distinct();


                    foreach (var myProduct in myProductList)
                    {
                        DropDownList_Product.Items.Add(new ListItem(myProduct.产品ID + "|" + myProduct.产品账户名称, myProduct.产品ID));

                    }

                }
            }
        }



        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenter = new ChildCenterList(CenterID);

            var myCenterFilters = myCenter.CostCenterlist.Where(p => p.停用 == false);


            foreach (var MylistItem in myCenterFilters)
            {

                CenterIdArray = CenterIdArray + "," + MylistItem.作业中心ID.ToString();

            }


        }

        protected void Button_QueryLine_Click(object sender, EventArgs e)
        {
            string MyAp = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;

            string MyProductId = DropDownList_Product.SelectedValue;
            var isValidAccountPeriod = from a in db.TABC_JS_AccountPeriods
                                       where a.会计期间ID == MyAp
                                       select a;
            if (isValidAccountPeriod.Count() == 0)
            {
                Response.Write("<script>alert('不存在指定的会计期间');</script>");
                return;

            }
            else
            {
                CostReport MyReport = new CostReport();
                string UserName = Session[ServerVars.sysSessionUserName].ToString();
                string myHtmlReport = "";

                if (CheckBox_WithInventary.Checked == false)

                    myHtmlReport = MyReport.WriteExcel_Report_CPZYZXCBHZB(MyProductId, MyAp, UserName,false,"yes",false);

                else
                {
                    myHtmlReport = MyReport.WriteExcel_Report_CPZYZXCBHZB(MyProductId, MyAp, UserName,true,"yes",false);
                }

                Response.Redirect("/Report/UserReport/" + myHtmlReport + ".html");
            }
        }

        protected void Button_Printe_Click(object sender, EventArgs e)
        {
            string MyAp = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;

            string MyProductId = DropDownList_Product.SelectedValue;
            var isValidAccountPeriod = from a in db.TABC_JS_AccountPeriods
                                       where a.会计期间ID == MyAp
                                       select a;
            if (isValidAccountPeriod.Count() == 0)
            {
                Response.Write("<script>alert('不存在指定的会计期间');</script>");
                return;

            }
            else
            {
                CostReport MyReport = new CostReport();
                string UserName = Session[ServerVars.sysSessionUserName].ToString();
                string myHtmlReport = "";

                if (CheckBox_WithInventary.Checked == false)

                    myHtmlReport = MyReport.WriteExcel_Report_CPZYZXCBHZB(MyProductId, MyAp, UserName, false, "yes", false);

                else
                {
                    myHtmlReport = MyReport.WriteExcel_Report_CPZYZXCBHZB(MyProductId, MyAp, UserName, true, "yes", false);
                }

                HttpResponse response = HttpContext.Current.Response;
                response.Clear();
                response.WriteFile("/Report/UserReport/" + myHtmlReport + ".xls");
                string httpHeader = "attachment;filename= CPZYZXCBHZB" + MyAp + ".Xls";
                response.AppendHeader("Content-Disposition", httpHeader);
                response.Flush();
            }
        }

        protected void Button_PrinteAll_Click(object sender, EventArgs e)
        {
            string MyEmptyString = "  ";
            for (int i = 0; i < 300; i++)
            {
                MyEmptyString = MyEmptyString + " ";
            }

            //判断会计期间的合法性。
            string MyAp = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;

            var isValidAccountPeriod = from a in db.TABC_JS_AccountPeriods
                                       where a.会计期间ID == MyAp
                                       select a;
            if (isValidAccountPeriod.Count() == 0)
            {
                Response.Write("<script>alert('不存在指定的会计期间');</script>");
                return;

            }

            //初始化需要使用的各参数。
            CostReport MyReport = new CostReport();
            string UserName = Session[ServerVars.sysSessionUserName].ToString();
            string myHtmlReport = "";
            bool AddRealInventory = CheckBox_WithInventary.Checked;

            Session["MyZipPath"] = "";
            string MyProductId = "";
            string MyfilePath = "";
            string MyRenamePath = "";
            string MyZipPath = "";

            MyZipPath = System.Web.HttpContext.Current.Server.MapPath("../../") + "Report\\cpzyzxcbhzb\\" + MyAp + UserName + ".zip";
            //判断压缩目标是否存在，存在的话，删除之。
            if (System.IO.File.Exists(MyZipPath))
            {

                System.IO.File.Delete(MyZipPath);

            }



            List<string> myfiles = new List<string>();

            int ProductTotalCount = DropDownList_Product.Items.Count;
            int CurrentCount = 0;

            foreach (ListItem myProduct in DropDownList_Product.Items)
            {

                MyProductId = myProduct.Value;


                myHtmlReport = MyReport.WriteExcel_Report_CPZYZXCBHZB(MyProductId, MyAp, UserName, true,"yes",false);
                if (myHtmlReport == "-1")
                {
                    Response.Write(MyEmptyString);//用于填充满256个字节。
                    Response.Flush();
                    //当报表不存在的时候，返回信息。
                    Response.Write("<div  align = center><br>" + "(" + CurrentCount.ToString() + "/" + ProductTotalCount + ")  " + MyProductId + "，无成本信息</div>");

                }
                else
                {
                    // 读取EXCEL表的路径信息。
                    MyfilePath = System.Web.HttpContext.Current.Server.MapPath("../../") + "Report\\UserReport\\" + myHtmlReport + ".xls";

                    if (System.IO.File.Exists(MyfilePath))
                    {
                        //将EXCEL表的名字更新成以产品ID命名，并添加到压缩文件目录中
                        MyRenamePath = System.Web.HttpContext.Current.Server.MapPath("../../") + "Report\\UserReport\\" + "(" + MyProductId + ")" + ".xls";
                        if (System.IO.File.Exists(MyRenamePath))
                        {

                            System.IO.File.Delete(MyRenamePath);

                        }

                        System.IO.File.Move(MyfilePath, MyRenamePath);
                        myfiles.Add(MyRenamePath);
                    }

                    Response.Write(MyEmptyString);//用于填充满256个字节。


                    Response.Write("<div  align = center><br>" + "(" + CurrentCount.ToString() + "/" + ProductTotalCount + ")  " + "正在生成产品账户ID" + MyProductId + "报表，请等待。。。</div>");
                    Response.Flush();
                }


                CurrentCount++;//计数器加一

                //  break;
            }


            AbcZip zip = new AbcZip();
            zip.ZipFiles(myfiles, MyZipPath);
            Session["MyZipPath"] = MyZipPath;
            //   Response.Redirect("Download_CPZYZXCBHZB.aspx");
            // <meta http-equiv='refresh' content='2; url=$url'>
            Response.Write("<div  align = center><br>全部生成完成<a href=Download_CPZYZXCBHZB.aspx>下载</a〉</div>");
            Response.Write("<script>timeout(self.location.href='Download_CPZYZXCBHZB.aspx',5000);</script>");
            //   Response.Redirect("/Report/cpzyzxcbhzb/" + MyAp + UserName + ".zip");

        }
    }
}
